<?php

namespace App\Http\Controllers;

use App\Http\Requests\IndexAdminAdressRequest;
use App\Http\Requests\StoreAdminAddressRequest;
use App\Http\Requests\UpdateAdminAddressRequest;
use App\Http\Resources\AdminAdressResource;
use App\Models\AdminAddress;
use Illuminate\Support\Facades\Auth;

class AdminAddressController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:sanctum');
    }

    /**
     * Display a listing of the resource.
     */
    public function index(IndexAdminAdressRequest $request)
    {
        $address = AdminAddress::query();
        if (!isSuperadmin()) {
            $address->where('admin_id', Auth::id());
        }

        $address->when($request->filled('name'), function ($query) use ($request) {
            $name = $request->input('name');
            return $query->where('name', 'like', "%$name%");
        });

        $data = $address->with('admins')->orderBy('created_at', 'DESC')->paginate(
            $request->query('pageSize'),
            ['*'],
            'page',
            $request->query('pageNum')
        );
        $response = AdminAdressResource::collection($data);
        $total = $response->total(); // 获取分页信息中的总数
        return $this->success('查询成功', $response, $total);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreAdminAddressRequest $request, AdminAddress $adminAddress)
    {
        $adminAddress->fill($request->input());
        $adminAddress->admin_id = Auth::id();
        $adminAddress->status = '1';
        $adminAddress->order = 1;
        // 获取当前用户
        if ($request->input('is_default') === '1') {
            $admin = Auth::user();
            // 找到当前默认地址并将其标记为非默认
            $admin->addresses()->where('is_default', '1')->update(['is_default' => '2']);
            // 将新的地址标记为默认地址
            $adminAddress->is_default = '1';
        }
        $adminAddress->save();

        return $this->success('添加地址成功', $adminAddress);
    }

    /**
     * Display the specified resource.
     */
    public function show(AdminAddress $adminAddress, $id)
    {
        $adminAddress = $adminAddress->findOrFail($id);
        $data = new AdminAdressResource($adminAddress->load('admins'));
        return $this->success('查询成功', $data);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(AdminAddress $adminAddress)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateAdminAddressRequest $request, AdminAddress $adminAddress, $id)
    {
        $adminAddress = $adminAddress->findOrFail($id);
        $this->authorize('update', $adminAddress);
        $adminAddress->fill($request->input());
        $adminAddress->admin_id = Auth::id();
        // 获取当前用户
        if ($request->input('is_default') === '1') {
            $admin = Auth::user();
            // 找到当前默认地址并将其标记为非默认
            $admin->addresses()->where('is_default', '1')->update(['is_default' => '2']);
            // 将新的地址标记为默认地址
            $adminAddress->is_default = '1';
        }

        $adminAddress->save();

        return $this->success('更新地址成功', $adminAddress);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(AdminAddress $adminAddress, $id)
    {
        $adminAddress = $adminAddress->findOrFail($id);
        $this->authorize('delete', $adminAddress);
        $adminAddress->delete();
        return $this->success('删除成功', $adminAddress);
    }
}
